<?php
namespace jy_wteamfee;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	/*
		核实分销商
	*/
	public function checkmember($mid=0,$isexi=false){
			$mid=$mid?:$_SESSION['uid'];
			cfc('core')->checklogin();
			if($_SESSION['container']=='wxwork'){
				$member=pdo_get('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid']));
			}else{
				$member=pdo_get('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			}
			$page_setting=$this->plugin_setting('page_setting');
			$page_setting['tagent']=$page_setting['tagent']?:'团长';
			if(!$member['id']){
				exi('您不是企业员工，无权操作'.$_SESSION['uid'],'error');
			}
			$_SESSION['workerid']=$member['id'];
			$team=$this->get('team',array('merchid'=>$_SESSION['onemerchid'],'workerid'=>$member['id']));
			if(!$team['id']){
				$this->apply_agent($member['id']);
			}
			return $member;
	}
	public function member_update($mid,$update=array()){
		pdo_update('core_members',$update,array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
	}
	/*
		申请成为团队长
	*/
	public function apply_agent($workerid){
		$team=$this->get('team',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'workerid'=>$workerid));
		if(!$team['id']){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'merchid'=>$_SESSION['onemerchid'],
				'workerid'=>$workerid,
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('team',$new_apply);
			$team['id']=$this->insertid();
		}
		return $team['id'];
	}
	/*
		审核团队长
	*/
	public function check_agent($agent_id,$status,$remark=''){
		
	}
	public function set_parent($mid,$fromuser=0,$isexi=false){
		
		return true;
	}
	/*
		禁用团长
	*/
	public function stop_agent($workerid){
		$this->update('team',array('stop'=>1),array('merchid'=>$_SESSION['onemerchid'],'workerid'=>$workerid));
	}
	/*
		恢复团长
	*/
	public function start_agent($workerid){
		$this->update('team',array('stop'=>0),array('merchid'=>$_SESSION['onemerchid'],'workerid'=>$workerid));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($workerid,$self=false){
		//
		//file_put_contents(ROOT_D.'/1.txt',11);
		if(!$workerid){
			return;
		}
		$agent=$this->get('team',array('merchid'=>$_SESSION['onemerchid'],'workerid'=>$workerid));
		if(!$agent['workerid']){
			return;
		}
		if($agent['status']!=1 && !$self){
			return;
		}
		if($agent['stop'] && !$self){
			return;
		}
		$level=$this->get('level',array('id'=>$agent['level_id']));
		$level['level']=$level['level']?:0;
		$parameter=array(
			'params'=>array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$level_up_types=$this->model('model')->current_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		$member_ids=$this->getteamids($agent['workerid']);
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=$this->setting['is_upone']?false:true;
			foreach($level_up_types as $type){
				//file_put_contents(ROOT_D.'/1.txt',$type."\n",FILE_APPEND);
			if($type=='order_price'){
				//团订单总金额（元）,团订单总数（个）
				$agent['order_price']=pdo_getsum('jy_wteamfee_order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids),'order_price');
				if($this->setting['is_upone']){
					if($agent['order_price']>=$conditions['order_price']){
						$up=true;
						break;
					}
				}else{
					if($agent['order_price']<$conditions['order_price']){
						$up=false;
						break;
					}
				}
			}elseif($type=='order_num'){
				//团订单总金额（元）,团订单总数（个）
				$agent['order_num']=pdo_count('jy_wteamfee_order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids));
				//file_put_contents(ROOT_D.'/1.txt',$agent['order_num']."order_num\n",FILE_APPEND);
				if($this->setting['is_upone']){
					if($agent['order_num']>=$conditions['order_num']){
						$up=true;
						break;
					}
				}else{
					if($agent['order_num']<$conditions['order_num']){
						$up=false;
						break;
					}
				}
			}elseif($type=='self_order_num'){
				//自购订单总数,自购订单总金额
				$agent['self_order_num']=pdo_count('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$agent['mid']));
				if($this->setting['is_upone']){
					if($agent['self_order_num']>=$conditions['self_order_num']){
						$up=true;
						break;
					}
				}else{
					if($agent['self_order_num']<$conditions['self_order_num']){
						$up=false;
						break;
					}
				}
			}elseif($type=='self_order_price'){
				//自购订单总数,自购订单总金额
				$agent['self_order_price']=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$agent['mid']),'price');
				//file_put_contents(ROOT_D.'/1.txt',$agent['self_order_price']."self_order_price\n",FILE_APPEND);
				if($this->setting['is_upone']){
					if($agent['self_order_price']>=$conditions['self_order_price']){
						$up=true;
						break;
					}
				}else{
					if($agent['self_order_price']<$conditions['self_order_price']){
						$up=false;
						break;
					}
				}
			}elseif($type=='commission_money'){
				$agent['commission_money']=pdo_getsum('jy_wteamfee_commission',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$agent['mid']),'money');
				if($this->setting['is_upone']){
					if($agent['commission_money']>=$conditions['commission_money']){
						$up=true;
						break;
					}
				}else{
					if($agent['commission_money']<$conditions['commission_money']){
						$up=false;
						break;
					}
				}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
				'status'=>1,
			);
			$this->update('team',$update,array('id'=>$agent['id']));
			$this->update_agent_level($agent['fromuser']);
		}
		return true;
	}
	public function check_cangetfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		if($type==5){
			for($i=0;$i<5;$i++){
				$res=$this->check_file($this->setting['daili_file'.$i],$i);
				if($res){
					return true;
				}
			}
		}else{
			for($i=0;$i<5;$i++){
				$res=$this->check_file($this->setting['ping_file'.$i],$i);
				if($res){
					return true;
				}
			}
		}
		return false;
	}
	public function check_file($file,$key=0){
			$mid=$_SESSION['uid'];
			if(!$file){
				return false;
			}
			//直推预选团队长多少人
			if($key==0){
				$area_num=$this->count('team',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid));
				if($file<=$area_num){
					return true;
				}
			}elseif($key==1){
				//分红金额满多少元
				$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$mid);
				$ji_money=$this->sum('commission',$params,'money');
				if($file<=$ji_money){
					return true;
				}
			}elseif($key==2){
				//二级团队长满多少人
				$agent1_ids=$this->getall('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$mid),array('mid'),'mid');
				$agent1_ids=array_keys($agent1_ids);
				$agent2_num=$this->count('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$agent1_ids));
				if($file<=$agent2_num){
					return true;
				}
				
			}elseif($key==3){
				//三级团队长满多少人
				$agent1_ids=$this->getall('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$mid),array('mid'),'mid');
				$agent1_ids=array_keys($agent1_ids);
				$agent2_ids=$this->getall('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$agent1_ids),array('mid'),'mid');
				$agent2_ids=array_keys($agent2_ids);
				$agent3_num=$this->count('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$agent2_ids));
				if($file<=$agent3_num){
					return true;
				}
				
			}elseif($key==4){
				//四级团队长满多少人
				$agent1_ids=$this->getall('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$mid),array('mid'),'mid');
				$agent1_ids=array_keys($agent1_ids);
				$agent2_ids=$this->getall('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$agent1_ids),array('mid'),'mid');
				$agent2_ids=array_keys($agent2_ids);
				$agent3_ids=$this->getall('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$agent2_ids),array('mid'),'mid');
				$agent3_ids=array_keys($agent3_ids);
				$agent4_num=$this->count('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$agent3_ids));
				if($file<=$agent4_num){
					return true;
				}
			}elseif($key==5){
				//商城自购金额满
				$buyfee=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'ispay'=>1),'price');
				if($file<=$buyfee){
					return true;
				}
			}elseif($key==6){
				//直推获得拼团金满
				if(!$this->plugin_exists('jy_pinggou')){
					return true;
				}
				$agent1_ids=$this->getall('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$mid),array('mid'),'mid');
				if($agent1_ids){
					$agent1_ids=array_keys($agent1_ids);
				}else{
					$agent1_ids=-1;
				}
				$fee=pdo_getsum('jy_pinggou_feelog',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$agent1_ids,'type'=>3),'fee');
				if($file<=$fee){
					return true;
				}
			}elseif($key==7){
				//直推累计购物满
				$agent1_ids=$this->getall('team',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$mid),array('mid'),'mid');
				if($agent1_ids){
					$agent1_ids=array_keys($agent1_ids);
				}else{
					$agent1_ids=-1;
				}
				$buyfee=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$agent1_ids,'ispay'=>1),'price');
				if($file<=$buyfee){
					return true;
				}
			}
		return false;
	}
	//能否再获得奖金
	public function check_canaddfee($mid=0,$type=5){
		return true;
	}
	public function check_isgetfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		$params=array(
			'merchid'=>$_SESSION['onemerchid'],
			'status'=>3,
			'fee_type'=>$type,
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
		);
		$balance=$this->get('balance',$params);
		if($balance['id']){
			return true;
		}else{
			return false;
		}
	}
	public function getteamids($workerid=0){
		$workerid=$workerid?:$_SESSION['workerid'];
		$mids=pdo_getall('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'workerid'=>$workerid),array('mid'),'mid');
		if($mids){
			$mids=array_keys($mids);
		}else{
			$mids=-1;
		}
		return $mids;
	}
}